﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CodeGenerator.Core;
using CodeGenerator.Common;
using System.IO;

namespace CodeGenerator.UI
{
    public partial class FrmFileOperation : Form
    {
        /// <summary>
        /// 文件是否操作成功
        /// </summary>
        public bool flag = false;

        private TemplateFiles entity;
        FileOperateType operateType;

        public FrmFileOperation(TemplateFiles entity, FileOperateType operateType)
        {
            InitializeComponent();
            this.entity = entity;
            this.operateType = operateType;
            if (operateType == FileOperateType.AddFile)
            {
                lbl_mark.Visible = true;
            }
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            errorProvider1.Clear();
            if (string.IsNullOrEmpty(txt_FileName.Text.Trim()))
            {
                errorProvider1.SetError(txt_FileName, "请输入文件名");
                txt_FileName.Focus();
                return;
            }

            string fileName=txt_FileName.Text.Trim();

            try
            {
                if (this.operateType == FileOperateType.AddDirection)
                {
                    if (fileName.Contains("."))
                    {
                        errorProvider1.SetError(txt_FileName, "请输入正确的文件名");
                        txt_FileName.Focus();
                        return;
                    }

                    if (entity.FileType== FileType.empty)
                    {
                        DirectoryUtil.CreateDir(entity.FullName + "\\" + fileName);
                    }
                    else
                    {
                        DirectoryUtil.CreateDir(Path.GetDirectoryName(entity.FullName) + "\\" + fileName);
                    }
                  
                    this.Close();
                }
                else if (this.operateType == FileOperateType.AddFile)
                {
                    if (fileName.Contains("."))
                    {
                        string[] Extension = fileName.Split('.');
                        string[] filter = { "cs", "java", "sql", "txt", "html", "cshtml", "jsp" };
                        Array.IndexOf(filter, 1);
                        if (!Utils.Contains(filter, Extension[Extension.Length - 1]))
                        {
                            errorProvider1.SetError(txt_FileName, "请输入正确的文件后缀名");
                            txt_FileName.Focus();
                        }
                        else
                        {
                            DirectoryUtil.CreateFile(Path.Combine(entity.FullName, fileName));
                            this.Close();
                        }
                    }
                }
                flag = true;
            }
            catch (Exception ex)
            {
                LogHelper.ErrorLog("创建自定义模板错误：",ex);
            }
        }
    }
}
